{include file="public/header"}
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <a class="layui-btn ht-event" title="添加频道" lay-href="{:url('Channel/channelAdd')}">添加频道</a>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <!--tree-->
            <table class="layui-table" id="LAY-auth-group" lay-filter="LAY-auth-group"></table>

        </div>
    </div>
</div>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" {{
           d.status== 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
    <a class="layui-btn  layui-btn-xs " lay-event="edit">编辑</a>
    {{# if (d.is_sys !=1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-disabled layui-btn-xs">删除</a>
    {{# } }}
</script>

{include file="public/common"}
<script>
    var tableId = 'LAY-auth-group';
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: '/lib/index',
    }).use(['index','layer','table', 'element'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , layer = layui.layer;

        // tree 渲染
        var ptable = table.render({
            elem: '#' + tableId
            , url: '{:url("Channel/channelList")}'
            , cols: [[
                {field: 'id', title: 'ID', fixed: true, width: 60, align: 'center'},
                {field: 'title', title: '频道名称'},
                {field: 'flag', title: '频道标识'},
                {field: 'status', title: '状态', templet:'#status'},
                {field: 'create_time', title: '添加时间'},
                {field: 'update_time', title: '更新时间'},
                {title: '操作', toolbar: '#action', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 25,
            page: true,
            id: 'table_id',
            method: 'post'
        });

        // 表格监听
        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data
                , layEvent = obj.event;
            // 编辑
            if (layEvent == 'edit') {
                layer.open({
                    type: 2
                    , title: "编辑"
                    , content: '{:url("Channel/channelEdit")}?id=' + data.id
                    , data: data
                    , area: ['90%', '90%']
                    , maxmin: true
                    , offset: ['50px' ,'100px']
                    , btn: ["确定", "取消"]
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'channel-edit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                type: 'POST'
                                , data: field
                                , url: "{:url('Channel/channelEdit')}"
                                , dataType: 'json'
                                , success: function (data) {
                                    if (data.code != 1) {
                                        layer.msg(data.msg, {icon: 2, time: 2500})
                                    } else {
                                        layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                                            table.reload('table_id'); //数据刷新
                                            layer.closeAll(); //关闭弹层
                                        })
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    },
                })
            }
            // 删除
            if (layEvent == 'del') {
                parent.layer.confirm('确定删除么?',function () {
                    $.post('{:url("Channel/channelDel")}', {'id': data.id}, function (data) {
                        if (data.code != 1) {
                            layer.msg(data.msg, {icon:2, time: 2500})
                        } else {
                            parent.layer.closeAll();
                            obj.del();
                        }
                    })
                });
            }

            // 配置规则
            if (layEvent == 'config') {
                var index = layer.open({
                    type: 2
                    , content: '{:url("Channel/groupRule")}?id=' + data.id
                    , title: '配置用户组--' + data.title + '--权限规则'
                })
                layer.full(index);
                // layer.closeAll();
            }
        });

        //监听搜索
        form.on('submit(LAY-auth-group-add)', function (data) {
            var field = data.field;
            $.post('{:url("Channel/groupAdd")}', field, function (data) {
                if (data.code != 1) {
                    layer.msg(data.msg, {time:2500, icon: 2})
                } else {
                    layer.msg(data.msg, {time:1000, icon: 1}, function () {
                        table.reload(tableId);
                    })
                }
            })
        });

        //监听开头 禁用管理员
        form.on('switch(status)', function (data) {
            loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("Channel/channelStatus")}', {'id': data.value}, function (data) {
                layer.close(loading);
                if (data.code != 1) {
                    layer.msg(data.msg, {icon: 2, time: 2 * 1000}, function () {
                        table.reload('table_id');
                    })
                } else {
                    //执行重载
                    //treeGrid.reload('LAY-tree-category');
                }
            });
        });

        // 事件
        var active = {
            userAdd: function () {
                layer.open({
                    type: 2
                    , content: '{:url("Channel/channelAdd")}'
                    , title: '添加管理员'
                    , area: ['50%', '80%']
                    , maxmin: true
                    , offset: ['50px' ,'100px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                type: 'POST'
                                , data: field
                                , url: "{:url('Channel/channelAdd')}"
                                , dataType: 'json'
                                , success: function (data) {
                                    if (data.code != 1) {
                                        layer.msg(data.msg, {icon: 2, time: 2500})
                                    } else {
                                        layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                                            table.reload('table_id'); //数据刷新
                                            layer.closeAll(); //关闭弹层
                                        })
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
        };

        $('.layui-btn.ht-event').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
